import { HttpClient } from '@angular/common/http'
import { Injectable, ɵConsole } from '@angular/core'
import { Location } from '@angular/common'
import { catchError } from 'rxjs/operators'
import { Observable } from 'rxjs'
import { NzModalService, NzMessageService } from 'ng-zorro-antd'
import { Subject } from 'rxjs'
import { MapConfig } from '../model/map.model'
declare var Cesium;
@Injectable()
export class InitCesiumService {
    /**
    * 绘制的对象
    */
    viewer: any; //视图
    id: any //图层id
    options: any //属性配置
    extent: any
    constructor(
        private location: Location
    ) {
        // this.initViewer(this.id,this.options)
    }


    initViewer(id, options: MapConfig) {
        this.id = id;
        this.options = Object.assign({}, this.getDefaultOptions(), options);
        this.viewer = new Cesium.Viewer(this.id, this.options);
        return this.viewer;
    }
    //获取Cesium的默认参数值
    getDefaultOptions() {
        return {
            navigationInstructionsInitiallyVisible: true,
            shouldAnimate: false,
            terrainExaggeration: 1.3,
            // imageryProvider: new Cesium.WebMapTileServiceImageryProvider({
            //     url: "http://t0.tianditu.com/img_w/wmts?tk=6ac4858482f0852842116eee4ff0574c",
            //     layer: 'img',
            //     style: 'default',
            //     tileMatrixSetID: 'w',
            //     format: 'tiles',
            //     maximumLevel: 18
            // }),
            imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
                name: "img_arcgis",
                url: "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
                maximumLevel: 500
            }),
            animation: false,              //是否创建动画小器件，左下角仪表
            baseLayerPicker: true,        //是否显示图层选择器
            fullscreenButton: false,       //是否显示全屏按钮
            geocoder: false,               //是否显示geocoder小器件，右上角查询按钮
            homeButton: false,             //是否显示Home按钮
            infoBox: true,                //是否显示信息框
            vrButton: false,                  //双屏模式,默认不显示false
            sceneModePicker: false,        //是否显示3D/2D选择器
            selectionIndicator: false,     //是否显示选取指示器组件
            timeline: false,               //是否显示时间轴
            navigationHelpButton: false,   //是否显示右上角的帮助按钮
            scene3DOnly: true,             //如果设置为true，则所有几何图形以3D模式绘制以节约GPU资源
            clock: new Cesium.Clock(),     //用于控制当前时间的时钟对象
            terrainProvider: new Cesium.EllipsoidTerrainProvider(),                  //地形图层提供者，仅baseLayerPicker设为false有意义
            creditContainer: document.createElement("div"),
            fullscreenElement: document.body,                   //全屏时渲染的HTML元素,
            useDefaultRenderLoop: true,                        //如果需要控制渲染循环，则设为true
            targetFrameRate: undefined,                         //使用默认render loop时的帧率
            showRenderLoopErrors: false,                        //如果设为true，将在一个HTML面板中显示错误信息
            automaticallyTrackDataSourceClocks: true,           //自动追踪最近添加的数据源的时钟设置
            contextOptions: undefined,                          //传递给Scene对象的上下文参数（scene.options）
            sceneMode: Cesium.SceneMode.SCENE3D,                //初始场景模式
            mapProjection: new Cesium.WebMercatorProjection(),  //地图投影体系
            dataSources: new Cesium.DataSourceCollection(),
            shadows: true
        }
    }
    //获取viewer对象
    getViewer() {
        if (this.viewer) {
            return this.viewer;
        } else {
            console.log("Viewer尚未初始化！");
        }
    }
    getCenterPosition() {
        var result = this.viewer.camera.pickEllipsoid(
            new Cesium.Cartesian2(
                this.viewer.canvas.clientWidth / 2,
                this.viewer.canvas.clientHeight / 2)
        );
        var curPosition = Cesium.Ellipsoid.WGS84.cartesianToCartographic(result);
        var lon = curPosition.longitude * 180 / Math.PI;
        var lat = curPosition.latitude * 180 / Math.PI;
        return {
            lon: lon,
            lat: lat,
            height: 0
        };
    }
    showInspector() {
        this.viewer.extend(Cesium.viewerCesiumInspectorMixin);
    }

    /**
     * 获取视角高度
     * @returns 
     */
    getHeight() {
        if (this.viewer) {
            var scene = this.viewer.scene;
            var ellipsoid = scene.globe.ellipsoid;
            var height = ellipsoid.cartesianToCartographic(this.viewer.camera.position).height;
            return height;
        }
    }

}
